package com.example.answer_staff;
import java.util.ArrayList;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gaosai.staff.utils.SPUtil;

public class FliterActivity  extends BaseActivity{

	private  ArrayList<Boolean> state_checkbox;

	private LinearLayout fliter_lineraylayout_main;
	private ArrayList<String> list_abbreString;
	private ArrayList<String> list_WholeString;
	private ArrayList<String> list_preference;
	private ArrayList<CheckBox> list_checkBox;
	private CheckBox checkbox_all;
	private SharedPreferences sp;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.fliter);
		setContentViewItem(R.layout.fliter_copy) ;
		setupView();
		geographicallayout();
		DisplaycheckBox();
		setlisteners();
	}

	/**
	 * 将上次选中的checkbox显示出来
	 */
	private void DisplaycheckBox() {
		for(int i=0;i<8;i++){
			String sp_value = sp.getString("sp_string"+i, "");
			System.out.println("sp——value是"+sp_value);
			if(list_WholeString.get(i).equals(sp_value)){
				list_checkBox.get(i).setChecked(true);
			}else{
				list_checkBox.get(i).setChecked(false);	
			}
		}
	}
	private void geographicallayout() {
		for(int i=0;i<8;i++){
			LinearLayout fliter_linearlayout_item=(LinearLayout) LayoutInflater.from(this).inflate(R.layout.fliter_linearlayout_dynamic, fliter_lineraylayout_main,false);
			//设置标签的名字和背景颜色
			TextView fliter_tagNames = (TextView)fliter_linearlayout_item.findViewById(R.id.fliter_tagNames);
			fliter_tagNames.setText(list_abbreString.get(i));
			fliter_tagNames.setBackgroundColor(setColor(i));
			//设置标签的全名。
			TextView fliter_textView = (TextView)fliter_linearlayout_item.findViewById(R.id.fliter_textView);
			fliter_textView.setText(list_WholeString.get(i));
			//只是需要判断checkbox的选中状态，
			CheckBox fliter_checkBox = (CheckBox)fliter_linearlayout_item.findViewById(R.id.fliter_checkBox);
			list_checkBox.add(fliter_checkBox);
			SelectedCheckBox(fliter_checkBox,list_WholeString.get(i),i);
			fliter_lineraylayout_main.addView(fliter_linearlayout_item);
		}
	}
	/**
	 * 判断哪一个checkbox被选中，就将checkbox对应的tagName的名字存放到sharedpreference里边。
	 * @param checkBox
	 * @param string
	 * @param i
	 */
	private void SelectedCheckBox(final CheckBox checkBox,final String string, final int i) {
		final Editor editor = sp.edit();
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(!isChecked){ 
					list_preference.remove(string);
					checkbox_all.setChecked(false);
					SPUtil.putValue(getApplicationContext(), SPUtil.CLICK, Boolean.toString(false));
					editor.putString("sp_string"+i, "");
					editor.commit();
				}else{
					list_preference.add(string.trim());
					editor.putString("sp_string"+i, string);
					editor.commit();
				}
				System.out.println("sp弟媳sh"+sp.getString("sp_string"+i, "没有取出值"));
				System.out.println("list_preference是"+list_preference.toString());
				System.out.println("state的checkBox是"+state_checkbox.toString());
			}
		});
	}
	/**
	 * 设置标签的颜色
	 * @param i  代表的是下标
	 * @return   返回的是颜色值
	 */
	private int setColor(int i) {
		int color=0;
		if(i==0){
			color=getApplicationContext().getResources().getColor(R.color.comm_color);	
		}else if(i==1){
			color=getApplicationContext().getResources().getColor(R.color.cs_color);	
		}else if(i==2){

			color=getApplicationContext().getResources().getColor(R.color.fin_color);	
		}else if(i==3){
			color=getApplicationContext().getResources().getColor(R.color.gtu_color);	

		}else if(i==4){
			color=getApplicationContext().getResources().getColor(R.color.hr_color);	

		}else if(i==5){
			color=getApplicationContext().getResources().getColor(R.color.inno_color);	

		}else if(i==6){
			color=getApplicationContext().getResources().getColor(R.color.ops_color);	
		}else{
			color=getApplicationContext().getResources().getColor(R.color.td_color);	
		}
		return color;
	}
	private void setlisteners() {
		//		checkbox_all.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		//			@Override
		//			public void onCheckedChanged(CompoundButton ViewBtton, boolean isCheck) {
		//				judgement(isCheck);
		//			}
		//		});
		checkbox_all.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if(checkbox_all.isChecked()){
					for(int i=0;i<8;i++){
						list_checkBox.get(i).setChecked(true);
					}
					list_preference.clear();
					list_preference.addAll(list_WholeString);
					SPUtil.putValue(getApplicationContext(), SPUtil.CLICK, Boolean.toString(true));
				Log.i("eee", "将值存进去了");
				
				}else{
					list_preference.clear();
					for(int i=0;i<8;i++){
						list_checkBox.get(i).setChecked(false);
					}
				}
			}
		});
	}

	/**
	 * 判断有没有选中所有的checkbox。
	 * @param isCheck
	 */
	protected void judgement(boolean isCheck) {

		if(isCheck){
			for(int i=0;i<8;i++){
				list_checkBox.get(i).setChecked(true);
			}
			list_preference.clear();
			list_preference.addAll(list_WholeString);
			SPUtil.putValue(getApplicationContext(), SPUtil.CLICK, true+"");

		}else{
			list_preference.clear();
			for(int i=0;i<8;i++){
				list_checkBox.get(i).setChecked(false);
			}
		}
	}
	private void setupView() {
		setTitleName("Fliter") ;
		hideTitlebtnAlignRight() ;
		showTitleBtnLeftAndRightImg(R.drawable.menu, R.drawable.menu, rightListener) ;
		fliter_lineraylayout_main=(LinearLayout)findViewById(R.id.Fliter_copy_linearlayout_main);
		checkbox_all=(CheckBox)findViewById(R.id.checkbox_all);
		SPUtil.getValue(getApplicationContext(), SPUtil.CLICK);
		Log.i("eee", "执行setupView的方法");
		Log.i("eee", "执行Sputil.getValue的结果是"+SPUtil.getValue(getApplicationContext(), SPUtil.CLICK));
		Log.i("eee", "执行Sputil.getValue转化成布尔的结果是"+Boolean.parseBoolean((SPUtil.getValue(getApplicationContext(), SPUtil.CLICK))));
		if(Boolean.parseBoolean((SPUtil.getValue(getApplicationContext(), SPUtil.CLICK)))){
			Log.i("eee", "执行setupView的if方法");
			checkbox_all.setChecked(true);	
		}else{
			
			Log.i("eee", "执行setupView的else方法");
			checkbox_all.setChecked(false);	
			
		}
		list_WholeString=new ArrayList<String>();
		list_abbreString=new ArrayList<String>();
		list_preference=new ArrayList<String>();
		list_checkBox=new ArrayList<CheckBox>();
		state_checkbox=new ArrayList<Boolean>();
		sp=PreferenceManager.getDefaultSharedPreferences(this);
		setWholeString();
		setabbreString();
	}
	private void setabbreString() {
		list_abbreString.add("COMM");
		list_abbreString.add("CS");
		list_abbreString.add("FIN");
		list_abbreString.add("GTU");
		list_abbreString.add("HR");
		list_abbreString.add("INNO");
		list_abbreString.add("OPS");
		list_abbreString.add("TD");
	}
	private void setWholeString() {
		list_WholeString.add("Communications");
		list_WholeString.add("Corporate Sustainability");
		list_WholeString.add("Finance");
		list_WholeString.add("Trade Union");
		list_WholeString.add("Human Resources");
		list_WholeString.add("Innovation");
		list_WholeString.add("Center Operations");
		list_WholeString.add("Technical Development");
	}


	//过滤tagNames
	private View.OnClickListener rightListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			SPUtil.putValue(FliterActivity.this, SPUtil.TAGNAMES, list_preference.toString());
			setResult(RESULT_OK);
			Log.e("list_preference", list_preference.toString()) ;
			finish();
		}
	};


}
